﻿from google.appengine.ext.webapp import template

class WebControl:
  def __init__(self):
    self.controls = []
    
  def add(self, control):
    self.controls.append(control)
    
  def get_child_controls_html(self):
    html = ""
    for control in self.controls:
      html += control.to_html()
    return html
  
  def to_html(self):
    pass

class HtmlTemplate(WebControl):
  def __init__(self, path, values):
    WebControl.__init__(self)
    self.path = path
    self.values = values
    
  def to_html(self):
    return template.render(self.path, self.values)
    
class HtmlText(WebControl):
  def __init__(self, text):
    WebControl.__init__(self)
    self.text = text
  
  def to_html(self):
    return self.text
    
class Html(WebControl):
  def __init__(self):
    WebControl.__init__(self)
  
  def to_html(self):
    html = "<html>\n"
    html += self.get_child_controls_html()
    html += "</html>\n"
    return html
    
    
class HtmlTitle(WebControl):
  def __init__(self, title):
    WebControl.__init__(self)
    self.title = title
    
  def to_html(self):
    return "<title>" + self.title + "</title>"

class HtmlBody(WebControl):
  def __init__(self):
    WebControl.__init__(self)
    
  def to_html(self):
    return "<body>" + self.get_child_controls_html() + "</body>"

class HtmlHeader(WebControl):
  def __init__(self):
    WebControl.__init__(self)
    
  def to_html(self):
    return "<head>" + self.get_child_controls_html() + "</head>"
  
class MessageList(WebControl):
  def __init__(self, message_list):
    WebControl.__init__(self)
    self.message_list = message_list
    
  def to_html(self):
    html = "<ul>\n"
    for message in self.message_list:
      html += "\t<li> " + message + " </li>\n"
    html += "</ul>\n"
    return html

class Banner(WebControl):
  
  def to_html():
    return '''<div class="container_24">
    <div class="grid=10">
    <h1>晚起床</h1>
    </div>
    <div class="grid=14">
    
    </div>
    <hr />
    ''' + self.get_child_controls_html() + '</div>'
    
class BasicHtml(Html):
  def __init__(self, html_body):
    Html.__init__(self)
    html_header = HtmlHeader()
    html_header.add(HtmlTitle("晚起床"))
    html_header.add(HtmlText("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>"))
    html_header.add(HtmlText('<link rel="stylesheet" href="/css/wanqichuang.css" type="text/css" media="screen" title="no title" charset="utf-8">'))
    html_header.add(HtmlText('<link rel="stylesheet" href="/css/960.css" type="text/css" media="screen" title="no title" charset="utf-8">'))
    self.add(html_header)
    self.add(html_body)
    